<?php
	$pagination_url = Router::getParam('pass');
	
	$blog_list = array(
		'title' =>
			'<div class="head">' .
				'<h1>' . __d('lil', 'Please select your blog', true) . '</h1>' .
			'</div>',
		'actions' => array(
			'pre' => '<div>',
			'post' => '</div>',
			'lines' => array(
			)
		),
		'table' => array(
			'pre' => '<div class="index">' . PHP_EOL,
			'post' => '</div>',
			'element' => array(
				'parameters' => array(
					'cellspacing' => 0,
					'cellpadding' => 0,
					'id'          => 'AdminBlogsList'
				),
			)
		)
	);

	$blog_list['table']['element']['head'] = array(
		'parameters' => array(),
		'rows' => array(
			0 => array(
				'parameters' => array(),
				'columns' => array(
					'id' => array(
						'parameters' => array('class' => 'center'),
						'html' => __d('lil', 'ID', true),
					),
					'name' => array(
						'parameters' => array('class' => 'left'),
						'html' => __d('lil', 'Name', true),
					),
					'slug' => array(
						'parameters' => array('class' => 'left'),
						'html' => __d('lil', 'Slug', true),
					),
					'description' => array(
						'parameters' => array('class' => 'left'),
						'html' => __d('lil', 'Description', true),
					),
				)
			)
		)
	);
	
	// table body values
	$blog_list['table']['element']['body']['rows'] = array();
	if (empty($data)) {
		$blog_list['table']['element']['body']['rows'][0] = array(
			'parameters' => array(),
			'columns' => array(
				0 => array(
					'parameters' => array('colspan' => '4', 'class' => 'light'),
					'html' => __d('lil', 'No blogs that match your criteria.', true)
				)
			),
		);
	} else {
		$i = 0;
		foreach($data as $item) {
			$blog_list['table']['element']['body']['rows'][$i] = array(
				'post' => $item,
				'parameters' => ($i++ % 2 == 0) ? array() : array('class' => 'altrow'),
				'columns' => array(
					'id' => array(
						'parameters' => array('class' => 'center'),
						'html' => $item['Blog']['id']
					),
					'name' => array(
						'parameters' => array('class' => 'big'),
						'html' => $this->Html->link(
							$item['Blog']['name'],
							array(
								'admin'      => true,
								'controller' => 'blogs',
								'action'     => 'select',
								$item['Blog']['id']
							)
						)
					),
					'slug' => array(
						'parameters' => array(),
						'html' => $this->Sanitize->html($item['Blog']['slug'])
					),
					'description' => array(
						'parameters' => array(),
						'html' => $this->Sanitize->html($item['Blog']['description'])
					),
				)
			);
			

		}
	}
	
	$blog_list['table']['element']['foot'] = array(
		'parameters' => array(),
		'rows' => array(
			0 => array(
				'parameters' => array(),
				'columns' => array(
					'paginator' => array(
						'parameters' => array('colspan' => '4', 'class' => 'paging'),
						'html' => 
							$this->Paginator->prev(
								'<< ' . __d('lil', 'previous', true),
								array('class' => 'prev', 'url' => $pagination_url),
								null,
								array('class'=>'prev light')
							) .
							$this->Paginator->next(
								__d('lil', 'next', true) . ' >>',
								array('class' => 'next', 'url' => $pagination_url),
								null,
								array('class'=>'next light')
							) .
							'<div class="counter">' . 
							$this->Paginator->counter(
								array('format' => __d('lil', 'Page <b>%page%</b> of <b>%pages%</b>, total <b>%count%</b> records.', true))
							) .
							'</div>'
					),
				)
			)
		)
	);
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// call plugin handlers
	$blog_list = $this->callPluginHandlers('admin_list_blogs', $blog_list);
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// form display begins
	echo $blog_list['title'] . PHP_EOL;
	
	echo $blog_list['actions']['pre'];
	foreach ($blog_list['actions']['lines'] as $name => $line) {
		if (is_array($line) && !empty($line['class'])) {
			$parameters = array();
			if (!empty($line['parameters'])) {
				$parameters = (array)$line['parameters'];
			}
			echo call_user_func_array(array($line['class'], $line['method']), $parameters);
		} else {
			echo $line;
		}
	}
	echo $blog_list['actions']['post'] . PHP_EOL;
	
	echo $blog_list['table']['pre'];
	echo '<table';
	foreach ($blog_list['table']['element']['parameters'] as $key => $param) {
		echo ' ' . $key . '="' . $param . '"';
	}
	echo '>' . PHP_EOL;
	
	// display thead
	echo '<thead';
	foreach ($blog_list['table']['element']['head']['parameters'] as $key => $param) {
		echo ' ' . $key . '="' . $param . '"';
	}
	echo '>' . PHP_EOL;
	
	foreach ($blog_list['table']['element']['head']['rows'] as $row) {
			echo '<tr';
			foreach ($row['parameters'] as $key => $param) {
				echo ' ' . $key . '="' . $param . '"';
			}
			echo '>' . PHP_EOL;
			
			foreach ($row['columns'] as $col) {
				echo '<th';
				foreach ($col['parameters'] as $key => $param) {
					echo ' ' . $key . '="' . $param . '"';
				}
				echo '>' . PHP_EOL;
				
				echo $col['html'];
				echo '</th>' . PHP_EOL;
			}
			
			echo '</tr>' . PHP_EOL;
	}
	echo '</thead>' . PHP_EOL;
	
	// display body
	foreach ($blog_list['table']['element']['body']['rows'] as $row) {
		echo '<tr';
		foreach ($row['parameters'] as $key => $param) {
			echo ' ' . $key . '="' . $param . '"';
		}
		echo '>' . PHP_EOL;
		
		foreach ($row['columns'] as $col) {
			echo '<td';
			foreach ($col['parameters'] as $key => $param) {
				echo ' ' . $key . '="' . $param . '"';
			}
			echo '>' . PHP_EOL;
			echo $col['html'];
			echo '</td>' . PHP_EOL;
		}
		
		echo '</tr>' . PHP_EOL;
	}
	
	// display tfoot
	echo '<tfoot';
	foreach ($blog_list['table']['element']['foot']['parameters'] as $key => $param) {
		echo ' ' . $key . '="' . $param . '"';
	}
	echo '>' . PHP_EOL;
	
	foreach ($blog_list['table']['element']['foot']['rows'] as $row) {
			echo '<tr';
			foreach ($row['parameters'] as $key => $param) {
				echo ' ' . $key . '="' . $param . '"';
			}
			echo '>' . PHP_EOL;
			
			foreach ($row['columns'] as $col) {
				echo '<td';
				foreach ($col['parameters'] as $key => $param) {
					echo ' ' . $key . '="' . $param . '"';
				}
				echo '>' . PHP_EOL;
				echo $col['html'];
				echo '</td>' . PHP_EOL;
			}
			
			echo '</tr>' . PHP_EOL;
	}
	echo '</tfoot>' . PHP_EOL;
	
	echo '</table>' . PHP_EOL;
	echo $blog_list['table']['post'];
?>